import { baseApiUrl, path } from "../../utils/config";
import { userPhb, standPhb } from '../../utils/api';
const app = getApp();
Page({
  data: {
    baseImageUrl: baseApiUrl,

    user_count: "",
    avatar: "",
    nickname: "",
    duration: "",

    field: "1",
    
    page: 1,
    last_page: 1,
    list: []
  },
  onLoad(options) {
    this.getStandPhb();
  },
  onReachBottom() {
    let page = this.data.page;
    let last_page = this.data.last_page;
    if (page == last_page) return false;
    page += 1;
    this.setData({ page });
    this.getList();
  },
  async getStandPhb() {
    let res = await standPhb({
      page: this.data.page,
      limit: 10
    });
    let stand_duration = res.stand_duration;
    let { head_img, nickname } = res.user;
    let avatar = head_img;
    let user_count = res.user_count;
    let { list, last_page } = res.list;
    list = this.data.page == 1 ? list : [...this.data.list, ...list];
    this.setData({ user_count, avatar, nickname, duration: stand_duration, list, last_page });
  },
  async getList() {
    let res = await userPhb({
      field: "stand_time",
      page: this.data.page,
      limit: 10
    });
    let { head_img, nickname, stand_time } = res.user;
    let avatar = head_img;
    let user_count = res.user_count;
    let { list, last_page } = res.list;
    list = this.data.page == 1 ? list : [...this.data.list, ...list];
    this.setData({ user_count, avatar, nickname, duration: stand_time, list, last_page });
  },
  changeActive(e) {
    let { field } = e.currentTarget.dataset;
    let page = 1, list = [];
    this.setData({ field, page, list });
    field == 1 ? this.getStandPhb() : this.getList();
  },
  onClickLeft() {
    wx.navigateBack()
  },
  onShareAppMessage() {
    let poster = app.globalData.poster;
    return {
      path,
      imageUrl: poster
    }
  },
  onShareTimeline() { }
})